package com.test.netty;

import io.netty.channel.ChannelOption;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.logging.LogLevel;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.netty.resources.LoopResources;

@Configuration
public class NettyConfig {

    @Bean
    public WebServerFactoryCustomizer<NettyReactiveWebServerFactory> nettyCustomizer() {
        return factory -> factory.addServerCustomizers(httpServer ->
            httpServer
                // 设置连接超时（毫秒）
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 10000)
                // 设置 backlog
                .tcpConfiguration(tcpServer -> tcpServer
                    .option(ChannelOption.SO_BACKLOG, 65535)
                    .childOption(ChannelOption.SO_REUSEADDR, true)
                )
                // 设置事件循环线程数
                .runOn(LoopResources.create("custom", 200, true))
                // 添加日志处理器（可选，用于调试）
                .doOnConnection(conn -> conn.addHandlerLast(new LoggingHandler(LogLevel.INFO)))
        );
    }
}
